package cn.zc.examination.service.service.impl;

import cn.tools.core.VO.PageVO;
import cn.tools.core.exception.BizException;
import cn.tools.core.utils.mybatis.PageUtil;
import cn.tools.dozer.config.DozerUtils;
import cn.zc.examination.entity.Dto.ExaminationBankDto;
import cn.zc.examination.entity.Dto.ExaminationOptionDto;
import cn.zc.examination.entity.ExaminationBank;
import cn.zc.examination.entity.ExaminationOption;
import cn.zc.examination.entity.VO.ExaminationBankVO;
import cn.zc.examination.service.mapper.ExaminationBankMapper;
import cn.zc.examination.service.service.ExaminationBankService;
import cn.zc.examination.service.service.ExaminationOptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 考试题库 服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-22
 */
@Service
public class ExaminationBankServiceImpl extends ServiceImpl<ExaminationBankMapper, ExaminationBank> implements ExaminationBankService {
    @Resource
    private DozerUtils dozerUtils;

    @Resource
    private ExaminationBankMapper examinationBankMapper;

    @Resource
    private ExaminationOptionService examinationOptionService;

    /**
     * 创建考题
     * @param examinationBankDto
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void createExaminationBank(ExaminationBankDto examinationBankDto) {
        ExaminationBank bank = dozerUtils.map(examinationBankDto, ExaminationBank.class);
        if(!save(bank)){
            throw new BizException("创建考题失败");
        }
        for(ExaminationOptionDto examinationOptionDto : examinationBankDto.getExaminationOption()){
            ExaminationOption option = dozerUtils.map(examinationOptionDto, ExaminationOption.class);
            option.setExaminationId(bank.getId());
            if(!examinationOptionService.createExaminationOption(option)){
                throw new BizException("创建考题选项失败");
            }
        }
    }



    /**
     * 修改考题
     * @param examinationBankDto
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void updateExaminationBank(ExaminationBankDto examinationBankDto) {
        ExaminationBank bank = dozerUtils.map(examinationBankDto, ExaminationBank.class);
        if(!save(bank)){
            throw new BizException("修改考题失败");
        }
        for(ExaminationOptionDto examinationOptionDto : examinationBankDto.getExaminationOption()){
            ExaminationOption option = dozerUtils.map(examinationOptionDto, ExaminationOption.class);
            option.setExaminationId(bank.getId());
            if(!examinationOptionService.updateExaminationOption(option)){
                throw new BizException("修改考题选项失败");
            }
        }
    }



    /**
     * 删除考题
     * @param id
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void deleteExaminationBank(String id) {
        if(!removeById(id)){
            throw new BizException("删除考题失败");
        }

        examinationOptionService.deleteExaminationOptionByBank(id);
    }


    /**
     * 条件查询考题
     * @param pageVO
     * @param examinationBankVO
     * @return
     */
    @Override
    public List<ExaminationBankDto> findExaminationBank(PageVO pageVO, ExaminationBankVO examinationBankVO) {
        List<ExaminationBankDto> bankDto = examinationBankMapper.findExaminationBank(PageUtil.initPage(pageVO),examinationBankVO);
        return bankDto;
    }
}
